﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Security;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using HtmlAgilityPack;
using CoursePack;
using Newtonsoft.Json;

namespace WindowsFormsApplication2
{
    public partial class MainForm : Form
    {


        private string Document;

        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

           
        }

        private void RequestClasses()
        {
           
            
        }


        private void FillTheGrid(string CurrentSession)
        {
            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.OptionFixNestedTags = true;
           
            htmlDoc.LoadHtml(Document);
            DataTable dttable = new DataTable();

            DataColumn column;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "CourseName";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "CourseNumber";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "CourseTitle";
            dttable.Columns.Add(column);




            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "LectureTime";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "LectureSec";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "LectureClass";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "LectureProf";
            dttable.Columns.Add(column);



            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "TutorialTime";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "TutorialSec";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "TutorialCLass";
            dttable.Columns.Add(column);




            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "LabTime";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "LabSec";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "LabCLass";
            dttable.Columns.Add(column);



            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Credits";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Session";
            dttable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Prerequisite";
            dttable.Columns.Add(column);


            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "SpecialNote";
            dttable.Columns.Add(column);

            DataRow row;

            foreach (HtmlNode input in htmlDoc.DocumentNode.SelectNodes("//table//tr"))
            {

                if (input.Attributes.Count > 1)
                    if ((input.Attributes[1].Value == "background-color:LightBlue;"))
                    {
                        

                        string CourseName, CourseNumber, CourseTitle, Session, Credits, Prerequisite = "",SpecialNote = "";

                        int spacePos = input.ChildNodes[2].InnerText.IndexOf(" ");

                        CourseName = input.ChildNodes[2].InnerText.Substring(0, spacePos);
                        CourseNumber = input.ChildNodes[2].InnerText.Substring(spacePos);
                        CourseTitle = input.ChildNodes[3].InnerText;
                        Credits = input.ChildNodes[4].InnerText;
     

                        HtmlNode pre = input.NextSibling;
                        bool noPrer = false;

                        while (pre != null && (!pre.InnerText.Contains("Prerequisite") || pre.InnerText.Contains(CurrentSession)))
                        {

                            if (pre.InnerText.Contains(CurrentSession))
                            {
                                noPrer = true;
                                break;
                            }

                            pre = pre.NextSibling;

                        }


                        if (!noPrer && pre != null)
                            Prerequisite = pre.ChildNodes[3].InnerText;

                        HtmlNode spec = input.NextSibling;
                        bool nospec = false;

                        while (spec != null && (!spec.InnerText.Contains("Special Note") || spec.InnerText.Contains(CurrentSession)))
                        {

                            if (spec.InnerText.Contains(CurrentSession))
                            {
                                nospec = true;
                                break;
                            }

                            spec = spec.NextSibling;

                        }

                        if (!nospec && spec != null)
                            SpecialNote = spec.ChildNodes[3].InnerText;

                        HtmlNode child = input.NextSibling;
                        while (child != null && !child.InnerText.Contains(CurrentSession))
                        {
                            child = child.NextSibling;

                        }

                        if (child == null)
                            continue;

                         HtmlNode Lecture;


                             Lecture = child;

                       
                        Session = CurrentSession;
                        
                        string LectureTime, LectureClass, LectureSec, LectureProf, 
                            TutorialTime = "", TutorialClass = "", TutorialSec = "",
                            LabTime = "", LabClass = "", LabSec = "";

                     

                       
                        while (Lecture.Attributes.Count > 1 && Lecture.Attributes[1].Value != "background-color:LightBlue;")
                        {
                            if (Lecture.InnerText.Contains("*Canceled*") 
                                || Lecture.InnerText.Contains("DEPARTMENT") 
                                || Lecture.InnerText.Contains("Department note")
                                || Lecture.InnerText.Contains("Department"))
                            {
                                Lecture = Lecture.NextSibling;
                                continue;
                            }

                            bool NotDone = true;
                            if (Lecture.ChildNodes[3].InnerText.Contains("Lect"))
                            {
                                LectureSec = Lecture.ChildNodes[3].InnerText.Substring(Lecture.ChildNodes[3].InnerText.IndexOf(" ") + 1);
                                LectureTime = Lecture.ChildNodes[4].InnerText;
                                LectureClass = Lecture.ChildNodes[5].InnerText;
                                LectureProf = Lecture.ChildNodes[6].InnerText;


                                while (Lecture.NextSibling.ChildNodes.Count > 2 && (Lecture.NextSibling.ChildNodes[3].InnerText.Contains("Tut") || Lecture.NextSibling.ChildNodes[3].InnerText.Contains("Lab")))
                                {
                                    if (Lecture.NextSibling.ChildNodes.Count > 2 && Lecture.NextSibling.ChildNodes[3].InnerText.Contains("Tut"))
                                    {
                                        TutorialSec = Lecture.NextSibling.ChildNodes[3].InnerText.Substring(Lecture.NextSibling.ChildNodes[3].InnerText.IndexOf(" ") + 1);
                                        TutorialTime = Lecture.NextSibling.ChildNodes[4].InnerText;
                                        TutorialClass = Lecture.NextSibling.ChildNodes[5].InnerText;

                                        if (Lecture.NextSibling.NextSibling.ChildNodes.Count > 2 && Lecture.NextSibling.NextSibling.ChildNodes[3].InnerText.Contains("Lab"))
                                        {

                                            if (Lecture.NextSibling.NextSibling.ChildNodes[3].InnerText.Contains("*Canceled*"))
                                            {
                                                Lecture = Lecture.NextSibling.NextSibling;
                                                continue;
                                            }

                                            LabSec = Lecture.NextSibling.NextSibling.ChildNodes[3].InnerText.Substring(Lecture.NextSibling.NextSibling.ChildNodes[3].InnerText.IndexOf(" ") + 1);
                                            LabTime = Lecture.NextSibling.NextSibling.ChildNodes[4].InnerText;
                                            LabClass = Lecture.NextSibling.NextSibling.ChildNodes[5].InnerText;

                                            row = dttable.NewRow();
                                            row["CourseName"] = CourseName;
                                            row["CourseNumber"] = CourseNumber;
                                            row["CourseTitle"] = CourseTitle;

                                            row["LectureTime"] = LectureTime;
                                            row["LectureSec"] = LectureSec;
                                            row["LectureClass"] = LectureClass;
                                            row["LectureProf"] = LectureProf;

                                            row["TutorialTime"] = TutorialTime;
                                            row["TutorialSec"] = TutorialSec;
                                            row["TutorialClass"] = TutorialClass;


                                            row["LabTime"] = LabTime;
                                            row["LabSec"] = LabSec;
                                            row["LabClass"] = LabClass;

                                            row["Credits"] = Credits;
                                            row["Session"] = Session;
                                            row["Prerequisite"] = Prerequisite;
                                            row["SpecialNote"] = SpecialNote;

                                            
                                            NotDone = false;
                                            dttable.Rows.Add(row);

                                            Lecture = Lecture.NextSibling.NextSibling;
                                            continue;
                                        }

     
                                    }

                                    else if (Lecture.NextSibling.ChildNodes.Count > 2 && Lecture.NextSibling.ChildNodes[3].InnerText.Contains("Lab"))
                                    {

                                        if (Lecture.NextSibling.ChildNodes[3].InnerText.Contains("*Canceled*"))
                                        {
                                            Lecture = Lecture.NextSibling;
                                            continue;
                                        }

                                        LabSec = Lecture.NextSibling.ChildNodes[3].InnerText.Substring(Lecture.NextSibling.ChildNodes[3].InnerText.IndexOf(" ") + 1);
                                        LabTime = Lecture.NextSibling.ChildNodes[4].InnerText;
                                        LabClass = Lecture.NextSibling.ChildNodes[5].InnerText;
                                    }


                                    row = dttable.NewRow();
                                    row["CourseName"] = CourseName;
                                    row["CourseNumber"] = CourseNumber;
                                    row["CourseTitle"] = CourseTitle;

                                    row["LectureTime"] = LectureTime;
                                    row["LectureSec"] = LectureSec;
                                    row["LectureClass"] = LectureClass;
                                    row["LectureProf"] = LectureProf;

                                    row["TutorialTime"] = TutorialTime;
                                    row["TutorialSec"] = TutorialSec;
                                    row["TutorialClass"] = TutorialClass;


                                    row["LabTime"] = LabTime;
                                    row["LabSec"] = LabSec;
                                    row["LabClass"] = LabClass;

                                    row["Credits"] = Credits;
                                    row["Session"] = Session;
                                    row["Prerequisite"] = Prerequisite;
                                    row["SpecialNote"] = SpecialNote;

                                    NotDone = false;
                                    dttable.Rows.Add(row);

                                    Lecture = Lecture.NextSibling;
                                }

                                if (NotDone)
                                {
                                    row = dttable.NewRow();
                                    row["CourseName"] = CourseName;
                                    row["CourseNumber"] = CourseNumber;
                                    row["CourseTitle"] = CourseTitle;

                                    row["LectureTime"] = LectureTime;
                                    row["LectureSec"] = LectureSec;
                                    row["LectureClass"] = LectureClass;
                                    row["LectureProf"] = LectureProf;

                                    row["TutorialTime"] = TutorialTime;
                                    row["TutorialSec"] = TutorialSec;
                                    row["TutorialClass"] = TutorialClass;


                                    row["LabTime"] = LabTime;
                                    row["LabSec"] = LabSec;
                                    row["LabClass"] = LabClass;

                                    row["Credits"] = Credits;
                                    row["Session"] = Session;
                                    row["Prerequisite"] = Prerequisite;
                                    row["SpecialNote"] = SpecialNote;

                                    dttable.Rows.Add(row);
                                }
                            }
                            Lecture = Lecture.NextSibling;
                            
                        }                 
                    }
                
            }
      
            this.dataGridView1.DataSource = dttable;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (object sess in this.cmbSession.Items)
            {
                foreach (object dep in this.cmbDepartment.Items)
                {
                    if (dep.GetType() == typeof(Department) && ((Department)dep).Id.ToString() == "405")
                    {
                        // Create the request
                        HttpWebRequest m_request = (HttpWebRequest)HttpWebRequest.Create("http://fcms.concordia.ca/fcms/asc002_stud_all.aspx");

                        // Creating the HEAD

                        m_request.Method = "POST";
                        m_request.ContentType = "application/x-www-form-urlencoded";
                        m_request.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                        m_request.KeepAlive = true;

                        m_request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0";
                        m_request.CookieContainer = new CookieContainer();



                        // Creating the message of the body
                        ASCIIEncoding encoding = new ASCIIEncoding();

                        string dept = "";

                        if (dep.GetType() == typeof(Department))
                        {
                            dept = "0" + ((Department)dep).Id.ToString();
                            string dataa = "__EVENTTARGET=ctl00%24PageBody%24btn_ShowScCrs&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=%2FwEPDwULLTE5NzE0Mjg1OTMPZBYCZg9kFgQCAw9kFgJmD2QWAgIBD2QWAgICDw8WAh4EVGV4dAUJMjAxMy0yMDE0ZGQCBQ9kFgICAQ9kFgQCAQ9kFgJmD2QWAmYPZBYCZg9kFgICAQ9kFgYCAQ9kFgICAg9kFgJmDxAPFgYeDURhdGFUZXh0RmllbGQFBWRlc2NyHg5EYXRhVmFsdWVGaWVsZAUJQ09ERVZBTFVFHgtfIURhdGFCb3VuZGdkEBUCBzIwMTMtMTQHMjAxMi0xMxUCBDIwMTMEMjAxMhQrAwJnZxYBZmQCAw9kFgICAg9kFgJmDxAPFgofAQUIRkFDX0RFUFQfAgUJQ09ERVZBTFVFHwNnHglCYWNrQ29sb3IKpAEeBF8hU0ICCGQQFTwOQVJUUyAmIFNDSUVOQ0UYLSBBcHBsaWVkIEh1bWFuIFNjaWVuY2VzCS0gQmlvbG9neRwtIENoZW1pc3RyeSBhbmQgQmlvY2hlbWlzdHJ5LC0gQ2xhc3NpY3MsIE1vZGVybiBMYW5ndWF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YYCAsacgYYCAt%2BfxdcMAsacxdcMAtX61tEPArz61tEPAtX6iqEOArz6iqEOAqGj5tUFAv%2B3naoNAqa3naoNAv%2B30fsPAqa30fsPAouRsNEMAtX6us0MAtX6%2FpoPArz6%2FpoPAv%2B3gYYCAv%2B3xdcMAqyOjMwNAqa3xdcMAouRjMwNAqa3jdcMAouR1M0NAufcmKoFAuezssQJAoSzssQJAouzssQJAo6zssQJAoGzssQJAr2zssQJAoqzssQJAruzssQJAonkuM8PAprElP0FArX3q6sKAr%2BcuY0NAsHjxcUKAqXjxcUKAqzjxcUKAtLvmtQCAtzW0NEHcC%2FTfey6DdfZJSTLa5y%2Bq%2BcGbJM%3D&ctl00%24PageBody%24ddl"
                            + "Year=" + this.cmbYear.SelectedItem
                            + "&ctl00%24PageBody%24ddl"
                            + "Sess=" + ((int)((ESession)sess) + 1).ToString()
                            + "&ctl00%24PageBody%24dd"
                            + "lLevl=" + this.cmbLevel.SelectedItem.ToString()[0]
                            + "&ctl00%24PageBody%24ddl"
                            + "Dept=" + dept
                            + "&ctl00%24PageBody%24txtCournam=&ctl00%24PageBody%24txtCournum=&ctl00%24PageBody%24txtKeyTtle=&ctl00%24PageBody%24ddlClaWeek=+&ctl00%24PageBody%24ddlClaDays=+&ctl00%24PageBody%24ddlTimeBtw=+&ctl00%24PageBody%24ddlCampus=+";

                            byte[] data = encoding.GetBytes(dataa);



                            // Write the stream to the server
                            using (Stream newStream = m_request.GetRequestStream())
                            {
                                newStream.Write(data, 0, data.Length);
                            }


                            using (StreamReader reader = new StreamReader(m_request.GetResponse().GetResponseStream()))
                            {

                                Document = reader.ReadToEnd();
                            }

                            FillTheGrid(((ESession)sess).ToString());

                            if (dep.GetType() == typeof(Department))
                            {
                                Department depp = (Department)dep;
                                string dir = "C:\\Users\\Public\\" + depp.Faculty.Id + "\\" + depp.Id;
                                if (!Directory.Exists(dir))
                                    System.IO.Directory.CreateDirectory(dir);

                                using (System.IO.StreamWriter file = new System.IO.StreamWriter(dir + "\\"
                                   + ((ESession)sess).ToString()
                                   + this.cmbYear.SelectedItem.ToString()
                                   + ".txt"))
                                {

                                    file.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                                    file.WriteLine(" Name | Number |                   Title                 | Lect Days | Start |  End  | Sect |        Class       |              Lecture Prof              | Tut Days | Start |  End  | Sect |         Class       | Lab Days | Start |  End  | Sect |        Class        |    Credits    | Session |                                        Prerequisite                                        |                                                                                 Special Note                                                                                ");
                                    file.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                                    foreach (DataGridViewRow row in this.dataGridView1.Rows)
                                    {
                                        if (row.Cells[0].Value != null)
                                        {
                                            string Lecture = row.Cells[3].Value.ToString();
                                            string Tutorial = row.Cells[7].Value.ToString();
                                            string Lab = row.Cells[10].Value.ToString();

                                            string CourseName = row.Cells[0].Value.ToString(),
                                                CourseNumber = row.Cells[1].Value.ToString(),
                                                CourseTitle = row.Cells[2].Value.ToString(),

                                                LectTime = Lecture.Substring(0, 7),
                                                LectStart = Lecture.Substring(9, 5),
                                                LectEnd = Lecture.Substring(15, 5),
                                                LectSec = row.Cells[4].Value.ToString(),
                                                LectClass = row.Cells[5].Value.ToString(),
                                                LectProf = row.Cells[6].Value.ToString();

                                            string TutTime = "", TutStart = "", TutEnd = "", TutSec = "", TutClass = "";
                                            if (Tutorial != "")
                                            {
                                                TutTime = Tutorial.Substring(0, 7);
                                                TutStart = Tutorial.Substring(9, 5);
                                                TutEnd = Tutorial.Substring(15, 5);
                                                TutSec = row.Cells[8].Value.ToString();
                                                TutClass = row.Cells[9].Value.ToString();
                                            }

                                            string LabTime = "", LabStart = "", LabEnd = "", LabSec = "", LabClass = "";
                                            if (Lab != "")
                                            {
                                                LabTime = Lab.Substring(0, 7);
                                                LabStart = Lab.Substring(9, 5);
                                                LabEnd = Lab.Substring(15, 5);
                                                LabSec = row.Cells[11].Value.ToString();
                                                LabClass = row.Cells[12].Value.ToString();
                                            }

                                            string Credits = row.Cells[13].Value.ToString(),
                                            Session = row.Cells[14].Value.ToString(),

                                            SpecialNotes = row.Cells[16].Value.ToString().Replace("\n", "").Replace("&nbsp;", "");

                                            string Prereq = row.Cells[15].Value.ToString().Replace("&nbsp;", "");


                                            string result = String.Format("{0,-5} | {1,-6} | {2,-39} | {3,-9} | {4,-5} | {5,-5} | {6,-4} | {7,-18} | {8,-38} | {9,-8} | {10,-5} | {11,-5} | {12,-4} | {13,-19} | {14,-8} | {15,-5} | {16,-5} | {17,-4} | {18,-19} | {19,13} | {20,7} | {21,-100} | {22,-200}",
                                                              CourseName, CourseNumber, CourseTitle, LectTime, LectStart, LectEnd, LectSec, LectClass, LectProf, TutTime, TutStart, TutEnd, TutSec, TutClass, LabTime, LabStart, LabEnd, LabSec, LabClass, Credits, Session, Prereq, SpecialNotes);


                                            file.WriteLine(result);
                                        }

                                    }
                                }
                            }
                            else
                            {
                                Faculty fac = (Faculty)dep;

                                if (!Directory.Exists("C:\\Users\\Public\\" + fac.Id))
                                    System.IO.Directory.CreateDirectory("C:\\Users\\Public\\" + fac.Id);
                            }

                        }
                        else
                        {
                            dept = "0" + ((Faculty)dep).Id.ToString();

                        }




                    }


                }
            }

      

           
            
        }

        private void cmbSession_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
            switch (this.cmbSession.SelectedItem.ToString())
            {
                case "Summer":
                    SessionNumber = "1";
                    CurrentSession = "Summer";
                    break;
                case "Fall":
                    SessionNumber = "2";
                    CurrentSession = "Fall";
                    break;
                case "Winter":
                    SessionNumber = "4";
                    CurrentSession = "Winter";
                    break;

            }
            CurrentSession = this.cmbSession.SelectedItem.ToString();*/
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Forms.ScheduleViewer frm = new Forms.ScheduleViewer(null);
            frm.ShowDialog();

        }

        private void button2_Click(object sender, EventArgs e)
        {
               
            System.IO.StreamReader file = new System.IO.StreamReader(@"CourseSequence.txt");
            
            file.ReadLine();
            file.ReadLine();
            file.ReadLine();
            string line;

    
            while ((line = file.ReadLine()) != null)
            {
                    if(line.Contains("Year"))
                    {

                    }
            }

                    
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            System.IO.StreamReader file = new System.IO.StreamReader(@"dep.txt");
            
            University concordia = new University();
            List<string> strs = new List<string>();
            string line;

            while ((line = file.ReadLine()) != null)
            {

                strs.Add(line);
            }
            
            bool done = false;
            bool flag = false;
            int j = 0;

            while (done == false)
            {
                CoursePack.Faculty f;
                CoursePack.Department d;

                string[] tab = strs[j].Split(';');

                if (tab[0].Length < 3 && tab.Length > 1)
                {
                    f = new Faculty(new University(),int.Parse(tab[0]), tab[1]);
                    concordia.AddFaculty(f);

                    while (flag == false && j + 1 != strs.Count)
                    {

                        string[] tab1 = strs[++j].Split(';');


                        if (tab1[0].Length > 2)
                        {
                            d = new Department(int.Parse(tab1[0]), tab1[1],f);
                            f.addDepartment(d);
                        }
                        else
                            break;
                    }

                }
                else
                    j++;

                if (j == strs.Count)
                    done = true;
            }

            foreach (Faculty f in concordia.Faculties)
            {
                this.cmbDepartment.Items.Add(f);

                foreach (Department d in f.Departments)
                {
                    this.cmbDepartment.Items.Add(d);
                }

            }
            foreach (ESession tmp in Enum.GetValues(typeof(ESession)))
            {
                this.cmbSession.Items.Add(tmp);
            }
            
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            
            
            
            
            
            
            
            // Create the request
            HttpWebRequest m_request = (HttpWebRequest)HttpWebRequest.Create("http://fcms.concordia.ca/fcms/asc002_stud_all.aspx");

            // Creating the HEAD

            m_request.Method = "GET";
            m_request.ContentType = "application/x-www-form-urlencoded";
            m_request.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            m_request.KeepAlive = true;

            m_request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0";
            m_request.CookieContainer = new CookieContainer();

            
            using (StreamReader reader = new StreamReader(m_request.GetResponse().GetResponseStream()))
            {

                Document = reader.ReadToEnd();
            }

            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.OptionFixNestedTags = true;
            int i = 0;
            htmlDoc.LoadHtml(Document);


            string dept = "";
            List<string> strs = new List<string>();

            foreach (HtmlNode input in htmlDoc.DocumentNode.SelectNodes("//select"))
            {

                if (input.Attributes[1].Value == "ctl00_PageBody_ddlDept")
                {
                    foreach (HtmlNode option in input.ChildNodes)
                    {
                        if (option.Attributes.Count > 0)
                        {
                           
                                dept = option.Attributes[option.Attributes.Count - 1].Value + ";";
                          
                                i++;
                        }
                        else if (option.InnerText != "\r\n\t\t\t\t\t\t\t")
                        {
                            if (option.InnerText.Contains('-'))
                                dept += option.InnerText.Substring(2);
                            else
                                dept += option.InnerText;
                            strs.Add(dept);
                        }
                        
                    }
                }
               
            }


            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"dep.txt"))
            {

                foreach (string str in strs)
                    file.WriteLine(str);
            }

            
        }

        private void button2_Click_1(object sender, EventArgs e)
        {

        }


       
            
    }
    
}
